<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <h2><el-icon><UserFilled /></el-icon> 个人信息</h2>
        </div>
      </template>
      
      <div class="user-info">
        <div class="avatar-section">
          <el-avatar 
            :size="100" 
            :icon="UserFilled"
            class="user-avatar"
          />
          <h3 class="username">{{ username }}</h3>
          <div class="user-role">管理员</div>
        </div>

        <el-divider>
          <el-icon><Lock /></el-icon> 安全设置
        </el-divider>

        <div class="password-change">
          <el-form 
            ref="passwordForm"
            :model="passwordForm"
            :rules="passwordRules"
            label-width="100px"
            class="password-form"
          >
            <el-form-item label="原密码" prop="oldPassword">
              <el-input 
                v-model="passwordForm.oldPassword"
                type="password"
                show-password
                prefix-icon="Lock"
              />
            </el-form-item>
            
            <el-form-item label="新密码" prop="newPassword">
              <el-input 
                v-model="passwordForm.newPassword"
                type="password"
                show-password
                prefix-icon="Key"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                :loading="loading"
                @click="handleChangePassword"
                class="submit-btn"
              >
                <el-icon><Check /></el-icon> 修改密码
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { userAPI } from '@/api/services'
import { UserFilled, Lock, Key, Check } from '@element-plus/icons-vue'

const store = useStore()
const loading = ref(false)

// 使用 computed 获取用户名
const username = computed(() => store.state.user.username)

const passwordForm = reactive({
  oldPassword: '',
  newPassword: ''
})

const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
}

const handleChangePassword = async () => {
  loading.value = true
  try {
    await userAPI.changePassword({
      old_password: passwordForm.oldPassword,
      new_password: passwordForm.newPassword
    })
    ElMessage.success('密码修改成功')
    passwordForm.oldPassword = ''
    passwordForm.newPassword = ''
  } catch (error) {
    ElMessage.error(error.message || '密码修改失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.profile-container {
  padding: 20px;
  min-height: 100vh;
  background: #f5f7fa;
}

.profile-card {
  max-width: 600px;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
}

.card-header h2 {
  margin: 0;
  font-size: 20px;
  color: #303133;
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar-section {
  text-align: center;
  padding: 20px 0;
}

.user-avatar {
  border: 4px solid #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  background: linear-gradient(45deg, #409eff, #36cfc9);
}

.username {
  margin: 15px 0 5px;
  font-size: 24px;
  color: #303133;
}

.user-role {
  color: #909399;
  font-size: 14px;
}

.password-form {
  max-width: 400px;
  margin: 20px auto;
}

.submit-btn {
  width: 100%;
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
  height: 40px;
  background: linear-gradient(45deg, #409eff, #36cfc9);
  border: none;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
}

:deep(.el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border-radius: 8px;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 2px 12px rgba(64, 158, 255, 0.2);
}

:deep(.el-divider__text) {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #606266;
  background: #f5f7fa;
  padding: 0 15px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-card {
    margin: 10px;
  }
  
  .password-form {
    padding: 0 20px;
  }
}
</style> 